package com.zhijian.medical.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.format.FastDateFormat;
import com.zhijian.medical.annotation.Response;
import com.zhijian.medical.config.CommonConfig;
import com.zhijian.medical.entity.dto.PersonDto;
import com.zhijian.medical.entity.vo.request.PersonSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.PersonSearchVo;
import com.zhijian.medical.entity.vo.response.CodeVo;
import com.zhijian.medical.entity.vo.response.PersonAllVo;
import com.zhijian.medical.entity.vo.response.PersonExamRecordVo;
import com.zhijian.medical.entity.vo.response.PersonVo;
import com.zhijian.medical.enums.PersonTypeEnum;
import com.zhijian.medical.enums.RoleNewEnum;
import com.zhijian.medical.holder.CurrentUserHolder;
import com.zhijian.medical.service.PersonGroupService;
import com.zhijian.medical.service.PersonService;
import com.zhijian.medical.util.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 人员 控制层
 *
 * @author HCG
 * @version 1.0
 * @date 2022-01-01 15:41:25
 */
@Slf4j
@Controller
@Api(tags = "人员")
public class PersonController {

    @Autowired
    private PersonService service;

    @Autowired
    private PersonGroupService personGroupService;

    @Autowired
    private CommonConfig commonConfig;

    /**
     * 获取最新编号们
     *
     * @return
     */
    @Response
    @GetMapping("/person/code/{type}")
    @ApiOperation("获取最新编号们")
    public CodeVo getNewCode(@PathVariable("type") Byte type) {
        CodeVo codeVo = new CodeVo();
        codeVo.setCode2(service.createNextCode(type));
        PersonTypeEnum personTypeEnum = PersonTypeEnum.getByCode(type.intValue());
        assert personTypeEnum != null;
        codeVo.setCode(personTypeEnum.getVersionCode());
        codeVo.setDw(commonConfig.getDw());
        codeVo.setVersion(personTypeEnum.getVersion());
        codeVo.setSaveTime("3年");
        codeVo.setZrr("文件管理员");
        codeVo.setPublishDate(DateUtil.format(new Date(), FastDateFormat.getInstance("yyyy年MM月dd日")));
        codeVo.setExecuteDate(codeVo.getPublishDate());
        return codeVo;
    }

    /**
     * 新增
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("person")
    @ApiOperation("新增")
    public boolean insert(@RequestBody PersonSaveOrUpdateVo param) {
        log.info("{} - {}| insert info", this.getClass().getSimpleName(), param);
        return service.insert(param) != 0;
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Response
    @DeleteMapping("person/{id}")
    @ApiOperation("删除")
    public boolean delete(@PathVariable("id") Long id) {
        log.info("{} - {}| delete info", this.getClass().getSimpleName(), id);
        service.deleteById(id);
        return true;
    }

    /**
     * 修改
     *
     * @param id
     * @param param
     * @return
     */
    @Response
    @PutMapping("person/{id}")
    @ApiOperation("修改")
    public boolean update(@PathVariable("id") Long id, @RequestBody PersonSaveOrUpdateVo param) {
        log.info("{} - {}| update info, {}", this.getClass().getSimpleName(), id, param);
        param.setId(id);
        return service.update(param);
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @Response
    @GetMapping("person/{id}")
    @ApiOperation("根据id查询")
    public PersonAllVo selectById(@PathVariable("id") Long id) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), id);
        return service.selectAllVoById(id);
    }

    /**
     * 分页查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("person/page")
    @ApiOperation("分页查询")
    public Page<PersonVo> selectByCondition(@RequestBody(required = false) PersonSearchVo param) {
        if (param == null)
            param = PersonSearchVo.builder().build();
        PersonDto user = CurrentUserHolder.getUser();
        if (Objects.nonNull(user) && user.getWeatherZxpx() == 1) {
            //如果是专项培训人员，只看自己
            param.setId(user.getId());
        }
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectPage(param);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @Response
    @PostMapping("person/list")
    @ApiOperation("列表查询，不分页")
    public List<PersonVo> selectList(@RequestBody(required = false) PersonSearchVo param) {
        if (param == null)
            param = PersonSearchVo.builder().build();
        PersonDto user = CurrentUserHolder.getUser();
        if (Objects.nonNull(user) && user.getWeatherZxpx() == 1) {
            //如果是专项培训人员，只看自己
            param.setId(user.getId());
        }
        log.info("{} - {}| query info", this.getClass().getSimpleName(), param);
        return service.selectList(param);
    }

    /**
     * 列表查询供应商人员
     *
     * @return
     */
    @Response
    @PostMapping("person/factory/list")
    @ApiOperation("列表查询供应商人员，不分页")
    public List<PersonVo> selectFactoryUserList() {
        return service.selectList(PersonSearchVo.builder()
                .roleIds(RoleNewEnum.GONG_YING_SHANG.getCodes())
                .build());
    }


    /**
     * 保存人员与组织的关系
     *
     * @param personId 人员id
     * @param groupId  组织id
     * @return
     */
    @Response
    @PostMapping("person/group/{personId}/{groupId}")
    @ApiOperation("保存人员与组织的关系")
    public boolean connectGroup(@ApiParam(name = "personId", value = "人员id") @PathVariable("personId") Long personId,
                                @ApiParam(name = "groupId", value = "组织id") @PathVariable("groupId") Long groupId) {
        return personGroupService.connectGroup(personId, groupId);
    }

    /**
     * 根据id查询
     *
     * @param jobNumber
     * @return
     */
    @Response
    @GetMapping("person")
    @ApiOperation("根据工号查询")
    public PersonVo selectByJobNumber(@ApiParam(name = "jobNumber", value = "工号")
                                      @RequestParam(value = "jobNumber") String jobNumber) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), jobNumber);
        return service.selectVoByJobNumber(jobNumber);
    }

    /**
     * 人员考核记录
     *
     * @param userId
     * @return
     */
    @Response
    @GetMapping("person/exam/record")
    @ApiOperation("人员考核记录")
    public List<PersonExamRecordVo> selectPersonExamRecordVo(@ApiParam(name = "userId", value = "人员id")
                                                             @RequestParam(value = "userId") Long userId) {
        log.info("{} - {}| query info", this.getClass().getSimpleName(), userId);
        return service.selectPersonExamRecordVoByUserId(userId);
    }

    /**
     * 在职人员导入excel
     *
     * @param file
     * @return
     */
    @com.zhijian.medical.annotation.Response
    @PostMapping("/person/job/import")
    @ApiOperation("在职人员导入excel")
    public boolean importExcel(MultipartFile file) {
        return service.importExcel(file);
    }

    /**
     * 在职人员导入excel
     *
     * @param file
     * @return
     */
    @com.zhijian.medical.annotation.Response
    @PostMapping("/person/fz/import")
    @ApiOperation("辅助人员导入excel")
    public boolean importFzExcel(MultipartFile file) {
        return service.importFzExcel(file);
    }

    /**
     * 修改签字图片
     *
     * @param personId
     * @param filePath
     * @return
     */
    @com.zhijian.medical.annotation.Response
    @PutMapping("/person/sign/pic/{personId}")
    @ApiOperation("修改签字图片")
    public boolean updateSignPic(@ApiParam(name = "personId", value = "人员id") @PathVariable("personId") Long personId,
                                 @ApiParam(name = "filePath", value = "文件路径") @RequestParam(value = "filePath") String filePath) {
        return service.updateSignPic(personId, filePath);
    }

}